package com.xc.common.tool;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Random;


//令牌生产器
public class TokenProcessor {
	private TokenProcessor(){}
	private static TokenProcessor instance = new TokenProcessor();
	public static TokenProcessor getInstance(){
		return instance;
	}
	public String generateTokeCode(){
		String value = System.currentTimeMillis()+new Random().nextInt()+"";
		//获取数据指纹，指纹是唯一的
		try {
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] b = md.digest(value.getBytes());//产生数据的指纹
			//Base64编码
//			BASE64Encoder be = new BASE64Encoder();
//			be.encode(b);
//			return be.encode(b);//制定一个编码
			Encoder encoder = Base64.getEncoder();
	        String encode = encoder.encodeToString(b);
			return encode;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	public static void main(String[] args) {
		TokenProcessor tokenProcessor = new TokenProcessor();
		System.out.println(tokenProcessor.generateTokeCode());
	}
}